package com.sofwin.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sofwin.mapper.SysRoleMapper;
import com.sofwin.mapper.SysRoleResourceMapper;
import com.sofwin.pojo.SysRole;
import com.sofwin.pojo.SysRoleExample;
import com.sofwin.pojo.SysRoleResource;
import com.sofwin.pojo.SysRoleResourceExample;
import com.sofwin.service.RoleService;
import org.assertj.core.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * @packageName: com.sofwin.service.impl
 * @author: andyliu
 * @date: 2022/5/4 14:56
 * @email 115176513@qq.com
 * @description: TODO
 */
@Service
@Transactional
public class RoleServiceImpl implements RoleService {
    @Autowired
    private SysRoleMapper mapper;
    @Autowired
    private SysRoleResourceMapper roleResourceMapper;

    @Override
    public boolean saveOrUpdateRole(SysRole role,Integer[] resourceIds) {
        boolean status = false;
        if(role.getRoleId()!=null){
            role.setUpdateDate(DateUtil.formatAsDatetime(new Date()));
            status= mapper.updateByPrimaryKeySelective(role)==1?true:false;
        }else {
            role.setCreateDate(DateUtil.formatAsDatetime(new Date()));
            status = mapper.insertSelective(role) == 1 ? true : false;
        }
        //roleId  resourceId
        // 根据roleId删除所有的sys_role_resource表中当前角色的所有资源
        SysRoleResourceExample deleExample = new SysRoleResourceExample();
        SysRoleResourceExample.Criteria criteria = deleExample.createCriteria();
        criteria.andRoleIdEqualTo(role.getRoleId());
        roleResourceMapper.deleteByExample(deleExample);
        int num=0;
        for(Integer resourceId:resourceIds){
            SysRoleResource roleResource = new SysRoleResource();
            roleResource.setRoleId(role.getRoleId());
            roleResource.setResourceId(resourceId);
            num+=roleResourceMapper.insertSelective(roleResource);
        }
        boolean flag = num==resourceIds.length?true:false;
        return status||flag;
    }

    @Override
    public SysRole queryById(Integer id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo<SysRole> queryRoles(SysRole role, Integer pageNumber, Integer pageSize) {
        PageHelper.startPage(pageNumber,pageSize);
        SysRoleExample example = new SysRoleExample();
        SysRoleExample.Criteria criteria = example.createCriteria();
        if(role!=null){
            if(!StringUtils.isEmpty(role.getRoleName())){
                criteria.andRoleNameLike("%"+role.getRoleName()+"%");
            }
            if(role.getStatus()!=null && role.getStatus()!=-1){
                criteria.andStatusEqualTo(role.getStatus());
            }
        }
        List<SysRole> roles = mapper.selectByExample(example);
        return new PageInfo<>(roles);
    }

    @Override
    public boolean removeById(Integer id) {
        return mapper.deleteByPrimaryKey(id)==1?true:false;
    }

    @Override
    public boolean removeByIds(Integer[] ids) {
        int flag = 0;
        for(Integer id:ids){
            boolean status = removeById(id);
            if(status){
                flag++;
            }
        }
        return flag==ids.length?true:false;
    }

    @Override
    public List<SysRole> queryAllRoles() {
        return mapper.selectByExample(null);
    }
}
